package com.chanjiedata.changbs.module.createcard;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.adapter.CreateShopAdapter;
import com.chanjiedata.changbs.base.BaseFragment;
import com.chanjiedata.changbs.help.UserHelper;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Merchantservice;
import com.chanjiedata.changbs.proto.Mystock;
import com.chanjiedata.changbs.proto.Order;
import com.chanjiedata.changbs.utlis.GsonUtils;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class CreateShopFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R.id.fragment_recycleView)
    RecyclerView mRecycleView;
    private int position;
    private List<Merchantservice.shop> dataBeans = new ArrayList<>();
    private CreateShopAdapter mAdapter;


    public static CreateShopFragment NewInstance(int instanceType) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", instanceType);
        CreateShopFragment fragment = new CreateShopFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    /**
     * 获取布局文件
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycleview;
    }


    /**
     * 初始化操作步骤
     */
    @Override
    protected void init() {
        unbinder = ButterKnife.bind(this, rootView);
        position = getArguments().getInt("position");
    }

    /**
     * 初始化界面
     */
    @Override
    protected void initView() {
        initRecycle();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        Merchantservice.shop_list.Builder builder = Merchantservice.shop_list.newBuilder();
        builder.setMemberId(UserHelper.getUser().getMember_id());
        if (position == 0) {
            builder.setType("0");
        } else {
            builder.setType("1");
        }
        NetworkManager.INSTANCE.post(Apis.merchant_list, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Merchantservice.shop_list list = Merchantservice.shop_list.parseFrom(response);
                List<Merchantservice.shop> datas = list.getListList();
                mAdapter.addData(datas);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }

    /**
     * 实例化线性底部RecyclerView
     * *
     */
    private void initRecycle() {
        mRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycleView.setAdapter(mAdapter = new CreateShopAdapter(mContext, dataBeans));
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setEmptyView(R.layout.recycle_empty);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mContext, CreateShopDetailActivity.class);
            intent.putExtra("id",dataBeans.get(position).getMerchantId());
            intent.putExtra("title",dataBeans.get(position).getMerchantName());
            intent.putExtra("type",this.position);
            startActivity(intent);
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("mango","OnResume");
        dataBeans.clear();
        mAdapter.replaceData(dataBeans);
        loadData();
    }
}
